package com.chengcang.api.modules.busi.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengcang.api.modules.busi.dao.BusiDemandDao;
import com.chengcang.api.modules.busi.dao.BusiOrderDao;
import com.chengcang.api.modules.busi.dto.*;
import com.chengcang.api.modules.busi.dto.orderSnapshotDTO.BusiOrderAddressSnapshotDTO;
import com.chengcang.api.modules.busi.dto.orderSnapshotDTO.BusiOrderInvoiceHeadSnapshotDTO;
import com.chengcang.api.modules.busi.entity.BusiDemandEntity;
import com.chengcang.api.modules.busi.entity.BusiOrderEntity;
import com.chengcang.api.modules.busi.service.*;
import com.chengcang.api.modules.busi.vo.BusiOrderDetailVo;
import com.chengcang.api.modules.busi.vo.BusiOrderVO;
import com.chengcang.api.modules.login.utils.UserHolder;
import com.chengcang.api.modules.sys.dao.*;
import com.chengcang.api.modules.sys.dto.SysProductDTO;
import com.chengcang.api.modules.sys.dto.orderSnapshotDTO.SysOrderProductSnapshotDTO;
import com.chengcang.api.modules.sys.entity.*;
import com.chengcang.api.modules.sys.service.SysDictDataService;
import com.chengcang.api.modules.sys.service.SysOrderProductSnapshotService;
import com.chengcang.api.modules.sys.service.SysProductService;
import com.chengcang.api.modules.sys.service.SysTenantService;
import com.chengcang.api.modules.sys.vo.SysProductSeriesVo;
import com.chengcang.common.enums.DataTypeEnum;
import com.chengcang.common.exception.CustomException;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@Service
@Slf4j
public class BusiOrderServiceImpl extends CrudServiceImpl<BusiOrderDao, BusiOrderEntity, BusiOrderRequestDTO> implements BusiOrderService {

    @Autowired
    private BusiOrderDao busiOrderDao;
    @Autowired
    private SysProductDao sysProductDao;

    @Autowired
    private BusiAddressService busiAddressService;

    @Autowired
    private BusiInvoiceHeadService busiInvoiceHeadService;

    @Autowired
    private SysProductService sysProductService;

    @Autowired
    private BusiDemandService busiDemandService;

    @Autowired
    private BusiDemandDao busiDemandDao;

    @Autowired
    private SysProductTypeDao sysProductTypeDao;

    @Autowired
    private SysTenantDao sysTenantDao;

    @Autowired
    private SysTenantService sysTenantService;

    @Autowired
    private SysDictTypeDao sysDictTypeDao;

    @Autowired
    private SysDictDataDao sysDictDataDao;

    @Autowired
    private SysDictDataService sysDictDataService;

    //--------------------------snapshot---------------------------
    @Resource
    private BusiDemandProductSnapshotService demandProductSnapshotService;

    @Resource
    private BusiDemandAddressSnapshotService demandAddressSnapshotService;

    @Resource
    private BusiDemandInvoiceHeadSnapshotService demandInvoiceHeadSnapshotService;

    @Resource
    private BusiOrderInvoiceHeadSnapshotService invoiceHeadSnapshotService;

    @Resource
    private SysOrderProductSnapshotService productSnapshotService;

    @Resource
    private BusiOrderAddressSnapshotService addrShotService;

    @Override
    public QueryWrapper<BusiOrderEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        QueryWrapper<BusiOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        return wrapper;
    }

    /**
     * todo 字典没解决， 货品类型没有
     * 自测完成
     * 需求: 排序
     *      筛选：货品类型，订单状态，单价范围，接单时间范围
     *      地址区域选择，需求时间范围
     *
     * @param dto
     * @return
     */
    @Override
    public Page<BusiOrderVO> getSupplyPage(BusiOrderPageDTO dto) {
        int count;
        //身份校验
        if(UserHolder.getUser().getDefaultUserType() == 1 || UserHolder.getUser().getDefaultUserType() == 2){
            dto.setSupplyId(UserHolder.getUser().getDefaultTenantId());
        }else {
            throw new CustomException("非公司&散户, 无法查看履约方订单信息!");
        }
        Page<BusiOrderVO> page = new Page<>();
        //货品分步筛选
//        if(dto.getProductOrTypeId() != null){
//            List<Long> productIds = this.setProductOrTypeId(dto.getProductOrTypeId());
//            if(productIds != null && productIds.size() != 0){
//                dto.setProductIds(productIds);
//            }else {
//                return page;
//            }
//        }

        if ((count = busiOrderDao.getSupplyCount(dto)) != 0) {
            List<BusiOrderVO> orders = busiOrderDao.getSupplyPage(dto);
            //构造产品信息
            for (BusiOrderVO order : orders) {
                order.setProductSeries(productSnapshotService.get(order.getProductId()));
            }
            page.setRecords(orders);
        }
        //this.dictListProcess(page);
        page.setTotal(count);
        return page;
    }

    @Override
    public Page<BusiOrderVO> getDemandOrderPage(BusiOrderPageDTO dto) {
        int count;
        //身份校验
        Integer tenantType = UserHolder.getUser().getDefaultUserType();
        if(tenantType == 0 || tenantType == 1){
            dto.setDemandId(UserHolder.getUser().getDefaultTenantId());
        }else {
            throw new CustomException("非工厂, 公司, 无法查看z支付方订单信息!");
        }
        Page<BusiOrderVO> page = new Page<>();
        //货品分步筛选
        if(dto.getProductOrTypeId() != null){
            List<Long> productIds = this.setProductOrTypeId(dto.getProductOrTypeId());
            if(productIds != null && productIds.size() != 0){
                dto.setProductIds(productIds);
            }else {
                return page;
            }
        }
        if ((count = busiOrderDao.getDemandOrderCount(dto)) != 0) {
            List<BusiOrderVO> orders = busiOrderDao.getDemandOrderPage(dto);
            //构造产品信息
            for (BusiOrderVO order : orders) {
                order.setProductSeries(productSnapshotService.get(order.getProductId()));
            }
            page.setRecords(orders);
        }
        //this.dictListProcess(page);
        page.setTotal(count);
        return page;
    }

    @Override
    public Page<BusiOrderVO> getAllOrderPage(BusiOrderPageDTO dto) {
        int count;
        Long tenantId = UserHolder.getUser().getDefaultTenantId();
        dto.setDemandId(tenantId);
        dto.setPaId(tenantId);
        Page<BusiOrderVO> page = new Page<>();
        //货品分步筛选
        if(dto.getProductOrTypeId() != null){
            List<Long> productIds = this.setProductOrTypeId(dto.getProductOrTypeId());
            if(productIds != null && productIds.size() != 0){
                dto.setProductIds(productIds);
            }else {
                return page;
            }
        }
        if ((count = busiOrderDao.getAllOrderCount(dto)) != 0) {
            List<BusiOrderVO> orders = busiOrderDao.getAllOrderPage(dto);
            //构造产品信息
            for (BusiOrderVO order : orders) {
                order.setProductSeries(productSnapshotService.get(order.getProductId()));
            }
            page.setRecords(orders);
        }
        //this.dictListProcess(page);
        page.setTotal(count);
        return page;
    }

    @Override
    public Long getUnsuppliedNum() {
        try {
            LambdaQueryWrapper<BusiOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(BusiOrderEntity::getSupplyId, UserHolder.getUser().getTenantList())
                    .isNotNull(BusiOrderEntity::getDeliveryInfo).eq(BusiOrderEntity::getDeleted, 0);
            //don't believe mysql server instance
            return Optional.ofNullable(busiOrderDao.selectCount(queryWrapper)).orElseGet(()->{return 0L;});
        }catch (Exception e){
            throw new CustomException("查询未履约量异常!");
        }
    }

    @Override
    public BusiOrderDetailVo detail(Long id) {
        //查询订单
        BusiOrderRequestDTO busiOrderDTO = this.get(id);
        Optional.ofNullable(busiOrderDTO).orElseThrow(() -> new CustomException("未找到对应的订单信息！"));
        BusiOrderDetailVo busiOrderDetailVo = new BusiOrderDetailVo();
        BeanUtils.copyProperties(busiOrderDTO, busiOrderDetailVo);
        BusiDemandEntity busiDemand;
        try {
            busiDemand = busiDemandDao.selectOne(new LambdaQueryWrapper<BusiDemandEntity>()
                    .eq(BusiDemandEntity::getDemandNo, busiOrderDTO.getDemandNo()));
        }catch (Exception e){
            log.error(e.getMessage(), e);
            throw new CustomException("获取需求异常,"+ e.getMessage());
        }
        Optional.ofNullable(busiDemand).orElseThrow(()->{return new CustomException("获取不到需求!");});
        //获取地址信息
        busiOrderDetailVo.setBusiAddressDTO(addrShotService.get(busiOrderDTO.getAddressId()));
        //获取发票信息
        busiOrderDetailVo.setBusiInvoiceHeadDTO(invoiceHeadSnapshotService.get(busiOrderDTO.getInvoiceHeadId()));
        //获取商品信息
        SysOrderProductSnapshotDTO product = productSnapshotService.get(busiOrderDTO.getProductId());
        this.productSpecDic(product);
        busiOrderDetailVo.setSysProductDTO(product);
        busiOrderDetailVo.setDemandExpire(busiDemand.getDemandExpire());
        busiOrderDetailVo.setPrice(busiDemand.getPrice());
        this.dictOneProcess(busiOrderDetailVo);
        //货品类型name
        if(busiOrderDetailVo.getProductUnit() != null){
            busiOrderDetailVo.setProductUnitName(sysDictDataService.getJoiningValue(DataTypeEnum.PRODUCT_UNIT,
                    busiOrderDetailVo.getProductUnit().split(",")));
        }
        if(StringUtils.isNotBlank(busiOrderDetailVo.getSysProductDTO().getSpec())) {
            busiOrderDetailVo.getSysProductDTO().setSpecDesc(sysDictDataService.getJoiningValue(DataTypeEnum.PRODUCT_UNIT,
                    busiOrderDetailVo.getSysProductDTO().getSpec().split(",")));
        }
        if(busiOrderDetailVo.getBusiInvoiceHeadDTO().getInvoiceType() != null){
            busiOrderDetailVo.getBusiInvoiceHeadDTO().setInvoiceTypeName(sysDictDataService.getValue(DataTypeEnum.INVOICE_TYPE,
                    String.valueOf(busiOrderDetailVo.getBusiInvoiceHeadDTO().getInvoiceType())));
        }
        return busiOrderDetailVo;

    }

    private void productSpecDic(SysOrderProductSnapshotDTO product){
        Long specTypeId = sysDictTypeDao.selectOne(new LambdaQueryWrapper<SysDictTypeEntity>().eq(SysDictTypeEntity::getDictType, "spec_type")).getId();
        Map<String, String> specDictMap = sysDictDataDao.selectList(new LambdaQueryWrapper<SysDictDataEntity>().eq(SysDictDataEntity::getDictTypeId, specTypeId))
                .stream().collect(Collectors.toMap(SysDictDataEntity::getDictValue, SysDictDataEntity::getDictLabel, (key1, kye2) -> {
                    return key1;
                }));
        if (product != null && product.getSpec() != null) {
            StringBuilder str = new StringBuilder();
            for (String dictValue : product.getSpec().split(",")) {
                str.append(specDictMap.get(dictValue)).append(",");
            }
            product.setSpecDesc(str.substring(0, str.length() - 1));
        }

    }


    @Override
    @Transactional
    public void saveOrder(BusiOrderRequestDTO dto) {
        //---------------------------------------save order---------------------------------------
        BigDecimal temp = dto.getSupplyNum();
        BusiDemandEntity demand = busiDemandDao.selectOne(new LambdaQueryWrapper<BusiDemandEntity>().eq(BusiDemandEntity::getDemandNo, dto.getDemandNo()));
        //校验
        Long tenantId = this.orderVerify(demand, dto);

        //组装订单数据
        BeanUtils.copyProperties(demand, dto);
        dto.setSupplyNum(temp);
        //订单总价
        dto.setTotalAmount(demand.getPrice().multiply(dto.getSupplyNum()));
        //构建 地址, 货品, 产品, 发票 信息。
        dto.setAddressId(this.saveAddrSnapshot(demand.getAddressId()));
        dto.setInvoiceHeadId(this.saveInvoiceHeadSnapshot(demand.getInvoiceHeadId()));
        dto.setProductId(this.saveProductSnapshot(demand.getProductId()));
        //默认代发货, 0: 待发货， 1：已发货
        dto.setStatus(0);
        //订单编号
        dto.setOrderNo(String.valueOf(NumberUtil.generateRandomNumber(1000, 10000, 1)[0]));
        BusiOrderEntity busiOrderEntity = new BusiOrderEntity();
        BeanUtils.copyProperties(dto, busiOrderEntity);
        busiOrderEntity.setId(null);
        busiOrderEntity.setTenantId(tenantId);
        //供应方
        busiOrderEntity.setSupplyId(tenantId);
        busiOrderEntity.setSupplyName(sysTenantService.get(UserHolder.getUser().getDefaultTenantId()).getName());
        //需求方
        busiOrderEntity.setDemandId(demand.getDemandId());
        busiOrderEntity.setDemandName(sysTenantService.get(demand.getDemandId()).getName());
        //甲方
        busiOrderEntity.setPaId(demand.getTenantId());
        busiOrderEntity.setPaName(sysTenantService.get(demand.getTenantId()).getName());
        //支付状态
        busiOrderEntity.setPayStatus(0);
        busiOrderEntity.setPayStatusName("待支付");
        //订单来源
        busiOrderEntity.setSourceType(1);
        busiOrderEntity.setSourceTypeName("APP");
        //save order
        busiOrderDao.insert(busiOrderEntity);

        //---------------------------------------更新需求---------------------------------------
        BusiDemandDTO busiDemandDTO = new BusiDemandDTO();
        busiDemandDTO.setId(demand.getId());
        //未履约
        BigDecimal supplyNum = demand.getAlreadySupplyNum() == null ? dto.getSupplyNum() : demand.getAlreadySupplyNum().add(dto.getSupplyNum());
        int flag = demand.getDemandTotal().compareTo(supplyNum);
        if (flag == 0) {
            //已完成
            busiDemandDTO.setDemandStatus(3);
        } else if(flag < 0){
            throw new CustomException("供应超量!");
        }
        busiDemandDTO.setPerformanceId(demand.getPerformanceId() == null ? tenantId.toString() : demand.getPerformanceId() + "," + tenantId);
        //总需求量 > 供应量
        busiDemandDTO.setAlreadySupplyNum(Optional.ofNullable(demand.getAlreadySupplyNum()).orElseGet(() -> {
            return new BigDecimal("0.0");
        }).add(dto.getSupplyNum()));
        busiDemandService.update(busiDemandDTO);
    }

    /**
     * 校验订单
     * @param demand
     * @param dto
     * @return
     */
    private Long orderVerify(BusiDemandEntity demand, BusiOrderRequestDTO dto){
        //校验
        Optional.ofNullable(demand).orElseThrow(()->{return new CustomException("查无此需求!");});
        BigDecimal maxPerformanceNum = demand.getMaxPerformanceNum();

        boolean maxFlag = maxPerformanceNum != null && maxPerformanceNum.compareTo(new BigDecimal("0.0")) != 0
                && maxPerformanceNum.compareTo(dto.getSupplyNum()) < 0;
        BigDecimal minPerformanceNum = demand.getMinPerformanceNum();
        boolean minFlag = minPerformanceNum != null && minPerformanceNum.compareTo(new BigDecimal("0.0")) != 0
                && minPerformanceNum.compareTo(dto.getSupplyNum()) > 0;
        if(maxFlag) throw new CustomException("超过最大履约量!");
        if(minFlag) throw new CustomException("小于最小履约量!");
        //工厂不能创建订单, 公司不能接自己的单
        Long tenantId;
        try {
            Optional.ofNullable(tenantId = UserHolder.getUser().getDefaultTenantId()).orElseThrow(()->new CustomException("没有租户信息!"));
            Integer defaultUserType = UserHolder.getUser().getDefaultUserType();
            if (defaultUserType == 0) throw new CustomException("工厂不能创建订单!");
            if (Objects.equals(demand.getTenantId(), tenantId)) throw new CustomException("不能接自己的单");
        }catch (NullPointerException e){
            throw new CustomException("没有租户信息!");
        }
        return tenantId;
    }

    /**
     * @param id
     * @desc
     */
    private Long saveInvoiceHeadSnapshot(Long id) {
        try {
            BusiDemandInvoiceHeadSnapshotDTO busiInvoiceHeadDTO = Optional.ofNullable(demandInvoiceHeadSnapshotService.get(id))
                    .orElseThrow(()->{return new CustomException("找不到发票抬头数据");});
            BusiOrderInvoiceHeadSnapshotDTO invoiceHeadSnapshotDTO = new BusiOrderInvoiceHeadSnapshotDTO();
            BeanUtils.copyProperties(busiInvoiceHeadDTO, invoiceHeadSnapshotDTO);
            invoiceHeadSnapshotDTO.setId(null);
            invoiceHeadSnapshotDTO.setInvoiceHeadId(busiInvoiceHeadDTO.getId());
            invoiceHeadSnapshotService.save(invoiceHeadSnapshotDTO);
            return invoiceHeadSnapshotDTO.getId();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CustomException("插入订单发票抬头快照异常!");
        }
    }


    /**
     * @param id
     * @desc
     */
    private Long saveAddrSnapshot(Long id) {
        try {
            BusiDemandAddressSnapshotDTO busiAddressDTO = Optional.ofNullable(demandAddressSnapshotService.get(id))
                    .orElseThrow(() -> {
                        return new CustomException("未找到地址数据");
                    });
            BusiOrderAddressSnapshotDTO addressSnapshotDTO = new BusiOrderAddressSnapshotDTO();
            BeanUtils.copyProperties(busiAddressDTO, addressSnapshotDTO);
            addressSnapshotDTO.setId(null);
            addressSnapshotDTO.setAddressId(busiAddressDTO.getId());
            addrShotService.save(addressSnapshotDTO);
            return addressSnapshotDTO.getId();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CustomException("插入订单地址快照异常!");
        }
    }

    /**
     * @param id
     * @desc
     */
    private Long saveProductSnapshot(Long id) {
        try {
            BusiDemandProductSnapshotDTO sysProductDTO = Optional.ofNullable(demandProductSnapshotService.get(id))
                    .orElseThrow(()-> {return new CustomException("未找到货品数据");});
            SysOrderProductSnapshotDTO addressSnapshotDTO = new SysOrderProductSnapshotDTO();
            BeanUtils.copyProperties(sysProductDTO, addressSnapshotDTO);
            addressSnapshotDTO.setId(null);
            addressSnapshotDTO.setProductId(sysProductDTO.getId());
            productSnapshotService.save(addressSnapshotDTO);
            return addressSnapshotDTO.getId();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CustomException("插入订单货品快照异常!");
        }
    }

    @Override
    @Transactional
    public void deliver(BusiOrderDeliverDTO dto) {
        //查询校验订单
        BusiOrderEntity busiOrderEntity;
        Optional.ofNullable(busiOrderEntity = selectById(dto.getId())).orElseThrow(() -> new CustomException(MessageFormat.format("该订单[{0}]不存在", dto.getId())));
        //查询需求
//        BusiDemandEntity busiDemandEntity = busiDemandDao.selectOne(new LambdaQueryWrapper<BusiDemandEntity>().eq(BusiDemandEntity::getDemandNo, busiOrderEntity.getDemandNo()));
//        Optional.ofNullable(busiDemandEntity).orElseThrow(() -> new CustomException(MessageFormat.format("该需求[{0}]不存在", busiOrderEntity.getDemandNo())));
//        BigDecimal totalNum = busiDemandEntity.getAlreadySupplyNum().add(busiOrderEntity.getSupplyNum());
//        if (busiDemandEntity.getDemandStatus() == 1) {
//            busiDemandEntity.setAlreadySupplyNum(totalNum.max(busiDemandEntity.getDemandTotal()));
//            busiDemandEntity.setDemandStatus(busiDemandEntity.getAlreadySupplyNum().equals(busiDemandEntity.getDemandTotal()) ? 2 : 1);
//        }
        //更新需求
//        BusiDemandDTO busiDemandDTO = new BusiDemandDTO();
//        BeanUtils.copyProperties(busiDemandEntity, busiDemandDTO);
//        busiDemandService.update(busiDemandDTO);
        //更新订单, 1: 已发货
        BusiOrderEntity order = BusiOrderEntity.builder().status(1).deliveryInfo(dto.getDeliveryInfo()).build();
        busiOrderDao.update(order, new LambdaQueryWrapper<BusiOrderEntity>().eq(BusiOrderEntity::getId, dto.getId()));

    }


    /**
     * 异步操作
     * @param supplier
     * @param executor
     * @param consumer
     * @return
     * @param <U>
     */
    private <U> CompletableFuture<U> doSync(Supplier<U> supplier, Executor executor, Consumer<U> consumer){
        return CompletableFuture.supplyAsync(supplier, executor).whenComplete((result, exception)->{
            consumer.accept(result);
        });
    }


    private List<Long> setProductOrTypeId(Long id){
        if(id != null){
            SysProductTypeEntity sysProductTypeEntity = sysProductTypeDao.selectById(id);
            List<Long> productTypeIds = Lists.newArrayListWithCapacity(7);
            if(sysProductTypeEntity != null && sysProductTypeEntity.getLevel() != null){
                if(sysProductTypeEntity.getLevel() == 1){
                    productTypeIds.add(id);
                }else if(sysProductTypeEntity.getLevel() == 0){
                    List<Long> ids = sysProductTypeDao.getProductsByTypeId(id).stream().map(SysProductEntity::getId).collect(Collectors.toList());
                    productTypeIds.addAll(ids);
                }
                if(productTypeIds.size() != 0){
                    return  sysProductDao.selectBatchIds(productTypeIds).stream().map(SysProductEntity::getId).distinct().collect(Collectors.toList());
                }
            }
        }
        return null;
    }

    protected void dictListProcess(Page<BusiOrderVO> pageData) {
        //字典处理
        List<Long> ids = pageData.getRecords().stream().map(BusiOrderVO::getDemandId).distinct().collect(Collectors.toList());
        ids.addAll(pageData.getRecords().stream().map(BusiOrderVO::getSupplyId).distinct().collect(Collectors.toList()));
        Map<Long, SysTenantEntity> tenantMap = sysTenantDao.selectList(new LambdaQueryWrapper<SysTenantEntity>().in(!ids.isEmpty(), SysTenantEntity::getId, ids))
                .stream().collect(Collectors.toMap(SysTenantEntity::getId, item -> {
                    return item;
                }));
        for (BusiOrderVO dto : pageData.getRecords()) {
            try {
                if (dto.getSupplyId() != null && tenantMap.get(dto.getSupplyId()) != null) {
                    dto.setSupplyName(tenantMap.get(dto.getSupplyId()).getName());
                }
                if (dto.getDemandId() != null && tenantMap.get(dto.getDemandId()) != null) {
                    dto.setDemandName(tenantMap.get(dto.getDemandId()).getName());
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new CustomException("字典转换异常！");
            }
        }
    }

    protected void dictOneProcess(BusiOrderDetailVo orderDto) {
        //字典处理
        ArrayList<Long> ids = Lists.newArrayList(orderDto.getDemandId(), orderDto.getSupplyId());
        Map<Long, SysTenantEntity> tenantMap = sysTenantDao.selectList(new LambdaQueryWrapper<SysTenantEntity>().in(!ids.isEmpty(), SysTenantEntity::getId, ids))
                .stream().collect(Collectors.toMap(SysTenantEntity::getId, item -> {
                    return item;
                }));
        try {
            if (orderDto.getSupplyId() != null && tenantMap.get(orderDto.getSupplyId()) != null) {
                orderDto.setSupplyName(tenantMap.get(orderDto.getSupplyId()).getName());
            }
            if (orderDto.getDemandId() != null && tenantMap.get(orderDto.getDemandId()) != null) {
                orderDto.setDemandName(tenantMap.get(orderDto.getDemandId()).getName());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CustomException("字典转换异常！");
        }

    }
}